package com.sunspring.zdcall.service.impl;

import com.sunspring.zdcall.bo.DeptBo;
import com.sunspring.zdcall.common.constant.enums.StateEnum;
import com.sunspring.zdcall.common.exception.ZdException;
import com.sunspring.zdcall.entity.ZdBaseCheckroom;
import com.sunspring.zdcall.mapper.master.CheckRoomMapper;
import com.sunspring.zdcall.service.CheckRoomService;
import com.sunspring.zdcall.service.DeptService;
import com.sunspring.zdcall.support.cache.DeptCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Author WuLei
 * @Date 2018/11/27  16:55
 */
@Service
@Slf4j
public class CheckRoomServiceImpl implements CheckRoomService {

    @Autowired
    private CheckRoomMapper crm;

    @Autowired
    private DeptService deptService;

    @Autowired
    private DeptCache deptCache;

    @Override
    public ZdBaseCheckroom getById(String id) {
        ZdBaseCheckroom checkroom = deptCache.getCr(id) ;
        if (checkroom != null) {
            return checkroom;
        }
        checkroom = new ZdBaseCheckroom();
        checkroom.setCheckRoomId(id);
        List<ZdBaseCheckroom> checkrooms = getSelective(checkroom);
        if (!CollectionUtils.isEmpty(checkrooms)) {
            checkroom = checkrooms.get(0);
            //检查室入缓存
            deptCache.setCr(checkroom);
            return checkrooms.get(0);
        }
        return null;
    }

    @Override
    public void addCheck(ZdBaseCheckroom checkRoomInfo) {
        int result = crm.insertCheckSelective(checkRoomInfo);
        if(result <= 0) {
            throw new ZdException("新增检查室信息失败，检查室:" + checkRoomInfo);
        }
        deptCache.setCr(checkRoomInfo);

    }

    @Override
    public void delete(String roomId) {
        int result = crm.deleteById(roomId);
        if (result <=0){
            throw new ZdException("删除检查室失败，检查室id："+roomId);
        }
        deptCache.removeCr(roomId);
    }

    @Override
    public void updateById(ZdBaseCheckroom checkRoomInfo) {
        int result = crm.updateById(checkRoomInfo);
        if (result <= 0){
            throw new ZdException("修改检查室信息失败，检查室i："+checkRoomInfo);
        }
        deptCache.setCr(checkRoomInfo);
    }

    @Override
    public ZdBaseCheckroom getByIdAndDeptId(String id, String deptId) {
        ZdBaseCheckroom checkroom = new ZdBaseCheckroom();
        checkroom.setCheckRoomId(id);
        checkroom.setDepartmentId(deptId);
        List<ZdBaseCheckroom> checkrooms = getSelective(checkroom);
        if (!CollectionUtils.isEmpty(checkrooms)) {
            return checkrooms.get(0);
        }
        return null;
    }

    @Override
    public List<ZdBaseCheckroom> getSelective(ZdBaseCheckroom checkroom) {
        return crm.selectSelective(checkroom);
    }

    @Override
    public void checkPrefix(String deptId, String prefix) {
        if (crm.selectByCheckIdAndPrefix(deptId,prefix) > 0) {
            throw new ZdException("相同科室下有重复的队列前缀");
        }
    }

    @Override
    public void switchCheckroom(String deptId,String checkroomId) {
        ZdBaseCheckroom checkroom = getById(checkroomId);
        int state = checkroom.getCheckroomState();
        int newState = state == StateEnum.enable.getCode() ? StateEnum.disable.getCode() : StateEnum.enable.getCode();
        //启用检查室
        if (newState == StateEnum.enable.getCode()) {
            //判断科室状态
            if (deptService.selectById(deptId).getDepartmentState()
                    == StateEnum.disable.getCode()) {
                throw new ZdException("所属科室已被停用,请先启用科室");
            }
        }
        checkroom.setCheckroomState(newState);
        if (crm.updateById(checkroom) <= 0) {
            throw new ZdException("操作检查室状态失败");
        }
        if (state == StateEnum.enable.getCode()) {
            log.info("检查室Id：" + checkroom.getCheckRoomId() + "已被停用");
        }
        if (state == StateEnum.disable.getCode()) {
            log.info("检查室Id " + checkroom.getCheckRoomId() + "已被启用");
        }
        deptCache.updateCrState(checkroomId,newState);

    }

    @Override
    public void deleteByDeptId(String deptId) {
        int result = crm.deleteByDeptId(deptId);
        if (result <= 0) {
            throw new ZdException("批量删除检查室");
        }
        log.info("已删除科室Id：{}下的所有检查室",deptId);
    }


    @Override
    public void updateStateByDeptId(String deptId, int code) {
        int result = crm.updateStateByDeptId(deptId,code);
        if (result <= 0) {
            throw new ZdException("批量停用检查室失败");
        }
        log.info("已停用科室Id:{}下的所有检查室",deptId);
    }


    @Override
    public List<ZdBaseCheckroom> getByDeptId(String deptId) {
        ZdBaseCheckroom checkroom = new ZdBaseCheckroom();
        checkroom.setDepartmentId(deptId);
        return getSelective(checkroom);
    }

    @Override
    public List<DeptBo> getAllAct() {
        return crm.selectDeptAndCheckroom();
    }
}
